﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Twos.Web.IService;
using Twos.Web.Service;

namespace Twos.Web.Controllers
{
    public class HomeController : Controller
    {
        //IOC属性调用[第一种调用方式]
        // public JobIservice _jobService { get; set; }


        //IOC构造函数调用[第二种调用方式]
        private readonly JobIservice _jobService;
        public HomeController(JobIservice jobService)
        {
            _jobService = jobService;
        }

        public ActionResult Error()
        {
           
            return View();
        }

        /// <summary>
        /// 数据页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Text() {

            //int.Parse("dffxg");
            return View();
        }
        /// <summary>
        /// 分页 查询数据方法
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult ShowJobList(int offset, int limit, string sort, string order, string name, string code)
        {
            int count = 0;   //总数据量
            if (String.IsNullOrWhiteSpace(sort))
            {
                sort = "Id";
            }
            var lst = _jobService.ShowJobList(offset, limit, sort, order, name, code, ref count);

            return Json(new { total = count, rows = lst }, JsonRequestBehavior.AllowGet);
        } 


        public ActionResult Index()
        {

            ////JobIservice 类名字             JobService类名字
            //JobIservice jobService = new JobService();
            var lst = _jobService.GetJobList();
            return Json(lst, JsonRequestBehavior.AllowGet);
        
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult Add(string name, string code, string index)
        {
            if (index == "")
            {
                return Json(_jobService.Add(name, code), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(_jobService.up(name, code, index), JsonRequestBehavior.AllowGet);
            }

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Del(string id)
        {
            var row = id.Split(',');
            int a = 0;
            foreach (var item in row)
            {
                if (_jobService.Del(id))
                {
                    a += 1;
                }
            }
            if (a == row.Length)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}